<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>登录日志</el-breadcrumb-item>
      <el-breadcrumb-item>日志列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 刷新列表按钮 -->
    <el-button type="primary" class="refreshBtn" @click="refresh"
      >刷新</el-button
    >
    <!-- 卡片视图 -->
    <el-card class="user-card">
      <!-- 搜索区 -->
      <el-row :gutter="20">
        <el-col :span="4">
          <el-input
            v-model="queryInfo.user_id"
            clearable
            placeholder="请输入用户id"
          >
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-input
            v-model="queryInfo.user_name"
            clearable
            placeholder="请输入用户名"
          >
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="search">搜索</el-button>
        </el-col>
      </el-row>
      <!-- 列表区 -->
      <el-table
        v-loading="loading"
        :data="logList"
        border
        stripe
        height="75vh"
        class="table"
      >
        <el-table-column type="index" label="#"></el-table-column>
        <el-table-column prop="user_name" label="用户名"></el-table-column>
        <el-table-column prop="user_id" label="用户ID"></el-table-column>
        <el-table-column prop="ip" label="登录IP"></el-table-column>
        <el-table-column label="登录时间">
          <template slot-scope="scope">
            {{ scope.row.time | dateFormat }}
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.page"
        :page-sizes="[1, 5, 10, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        style="margin-top: 20px"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
import moment from "moment";
import Vue from "vue";
Vue.filter("dateFormat", function (daraStr, pattern = "YYYY-MM-DD HH:mm:ss") {
  return moment(daraStr).format(pattern);
});
export default {
  data() {
    return {
      // 搜索参数
      queryInfo: {
        user_id: "",
        user_name: "",
        pagesize: 10,
        page: 1,
      },
      // 所有登录信息
      logList: [],
      // 加载的显示影藏
      loading: false,
      // 总条数
      total: 0,
    };
  },
  created() {
    this.getLogsList();
  },
  methods: {
    // 获取登录日志列表
    async getLogsList() {
      this.loading = true;
      const { data: res } = await this.$http.get("/api/logs", {
        params: this.queryInfo,
        ...this.getToken,
      });
      if (res.code !== 1) return this.$message.error("获取日志列表信息失败！");
      this.loading = false;
      this.logList = res.data.list;
      this.total = res.data.total;
      console.log(this.logList);
    },
    // 搜索
    search() {
      this.queryInfo.page = 1;
      this.getLogsList();
    },
    // 刷新
    refresh() {
      this.queryInfo.user_id = this.queryInfo.user_name = "";
      this.getLogsList();
    },
    // 监听数量改变
    handleSizeChange(newPageSize) {
      this.queryInfo.pagesize = newPageSize;
      this.getLogsList();
    },
    // 监听页数改变
    handleCurrentChange(newPage) {
      this.queryInfo.page = newPage;
      this.getLogsList();
    },
  },
  computed: {
    getToken() {
      return { headers: { authorization: sessionStorage.getItem("token") } };
    },
  },
};
</script>

<style>
</style>